package me.obstsalat.guildera;

import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.inventory.BrewEvent;
import org.bukkit.event.inventory.FurnaceSmeltEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.player.PlayerChatEvent;
import org.bukkit.event.player.PlayerExpChangeEvent;
import org.bukkit.event.player.PlayerItemHeldEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerPickupItemEvent;

public class hook_manager  implements Listener {
	private guildera plugin;
	
	hook_manager(guildera pl) {
		plugin = pl;
	}
	
	@EventHandler(priority = EventPriority.LOWEST)
	public void onPlayerChat(PlayerChatEvent chat) {
		if(!chat.isCancelled()) {
			boolean end = false;
			if(plugin.config.getBool("use_dungeons")) {
				end = plugin.i_d_chat.onPlayerChat(chat);
				if(end) return;
			}
			if(plugin.config.getBool("use_guilds")) {
				end = plugin.i_g_chat.onPlayerChat(chat);
				if(end) return;
			}
			if(!end) {
				if(chat.getMessage().startsWith(".")) {
					chat.setCancelled(true);
					chat.getPlayer().sendMessage("Unkown command. Type .help or .commands in chat to which commands you can use.");
				}
			}
		}
	}
	
	@EventHandler(priority = EventPriority.LOWEST)
	public void onBlockPlace(BlockPlaceEvent event) {
		if(!event.isCancelled() && event.canBuild()) {
			boolean end = false;
			if(plugin.config.getBool("use_dungeons")) {
				end = plugin.i_d_protection.onBlockPlace(event);
				if(end) return;
			}
			if(plugin.config.getBool("use_guilds")) {
				end = plugin.i_g_war.onBlockPlace(event);
				if(end) return;
				if(plugin.config.getBool("use_block_restriction")) {
					end = plugin.i_g_block_res.onBlockPlace(event);
					if(end) return;
				}
			}
		}
	}
	
	@EventHandler(priority = EventPriority.LOWEST)
	public void onBlockBreak(BlockBreakEvent event) {
		if(!event.isCancelled()) {
			boolean end = false;
			if(plugin.config.getBool("use_guilds")) {
				end = plugin.i_g_irongolem.onBlockBreak(event);
				if(end) return;
			}
			if(plugin.config.getBool("use_dungeons")) {
				end = plugin.i_d_protection.onBlockBreak(event);
				if(end) return;
			}
			if(plugin.config.getBool("use_guilds")) {
				end = plugin.i_g_war.onBlockBreak(event);
				if(end) return;
				end = plugin.i_g_experience.onBlockBreak(event);
				if(end) return;
			}
		}
	}
	
	@EventHandler(priority = EventPriority.LOWEST)
	public void onEntityExplode(EntityExplodeEvent event) {
		if(!event.isCancelled()) {
			boolean end = false;
			if(plugin.config.getBool("use_dungeons")) {
				end = plugin.i_d_protection.onEntityExplode(event);
				if(end) return;
			}
			if(plugin.config.getBool("use_guilds")) {
				end = plugin.i_g_war.onEntityExplode(event);
				if(end) return;
			}
		}
	}
	
	@EventHandler(priority = EventPriority.LOWEST)
    public void onInventoryClose(InventoryCloseEvent event) {
		if(plugin.config.getBool("use_guilds")) {
			if(plugin.config.getBool("use_armor_restriction")) {
				plugin.i_g_armor_res.onInventoryClose(event);
			}
			if(plugin.config.getBool("use_tool_restriction")) {
				plugin.i_g_tool_res.onInventoryClose(event);
			}
			plugin.i_g_war.onInventoryClose(event);
		}
	}
	
	@EventHandler(priority = EventPriority.LOWEST)
	public void onPlayerJoin (PlayerJoinEvent event) {
		if(plugin.config.getBool("use_guilds")) {
			if(plugin.config.getBool("use_armor_restriction")) {
				plugin.i_g_armor_res.onPlayerJoin(event);
			}
			if(plugin.config.getBool("use_tool_restriction")) {
				plugin.i_g_tool_res.onPlayerJoin(event); 
			}
			plugin.i_g_war.onPlayerJoin(event);
		}
	}
	
	@EventHandler(priority = EventPriority.LOWEST)
	public void onBrew(BrewEvent event) {
		if(!event.isCancelled()) {
			boolean end = false;
			if(plugin.config.getBool("use_guilds")) {
				end = plugin.i_g_experience.onBrew(event);
				if(end) return;
			}
		}
	}
	
	@EventHandler(priority = EventPriority.LOWEST)
	public void onPlayerDeath(PlayerDeathEvent event)  {
		if(plugin.config.getBool("use_guilds")) {
			plugin.i_g_experience.onPlayerDeath(event);
		}
	}
	
	@EventHandler(priority = EventPriority.LOWEST)
	public void onPlayerExpChange(PlayerExpChangeEvent event) {
		if(plugin.config.getBool("use_guilds")) {
			plugin.i_g_experience.onPlayerExpChange(event);
		}
	}
	
	@EventHandler(priority = EventPriority.LOWEST)
	public void onFurnaceSmelt(FurnaceSmeltEvent event) {
		if(!event.isCancelled()) {
			boolean end = false;
			if(plugin.config.getBool("use_guilds")) {
				end = plugin.i_g_experience.onFurnaceSmelt(event);
				if(end) return;
			}
		}
	}
	
	@EventHandler(priority = EventPriority.LOWEST)
	public void onPlayerItemHeld(PlayerItemHeldEvent event) {
		if(plugin.config.getBool("use_guilds")) {
			if(plugin.config.getBool("use_tool_restriction")) {
				plugin.i_g_tool_res.onPlayerItemHeld(event);
			}
			plugin.i_g_war.onPlayerItemHeld(event);
		}
	}
	
	@EventHandler(priority = EventPriority.LOWEST)
	public void onPlayerPickupItem(PlayerPickupItemEvent event) {
		if(!event.isCancelled()) {
			boolean end = false;
			if(plugin.config.getBool("use_guilds")) {
				end = plugin.i_g_war.onPlayerPickupItem(event);
				if(end) return;
				if(plugin.config.getBool("use_tool_restriction")) {
					end = plugin.i_g_tool_res.onPlayerPickupItem(event);
					if(end) return;
				}
			}
		}
	}
	
	@EventHandler(priority = EventPriority.LOWEST)
	public void onEntityDamagebyEntity(EntityDamageByEntityEvent event) {
		if(!event.isCancelled()) {
			boolean end = false;
			if(plugin.config.getBool("use_guilds")) {
				end = plugin.i_g_war.onEntityDamagebyEntity(event);
				if(end) return;
				end = plugin.i_g_irongolem.onEntityDamagebyEntity(event);
				if(end) return;
			}
		}
	}
}
